package com.nothing.controller;

import com.nothing.Model.User;
import com.nothing.Service.UserServiceImpl;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2017/7/12.
 * 参考文章：https://www.jianshu.com/p/1730df498855
 */
@Controller
public class OneFunc{
    private UserServiceImpl impl=new UserServiceImpl();
    @RequestMapping(value = "/one",method = RequestMethod.GET)
    public String printlnOne(Model model){
        model.addAttribute("msg","hello nan");
        return "one.jsp";
        }

        @RequestMapping(value = "/add",method = RequestMethod.GET)
        @ResponseBody
        public User addUser(@ModelAttribute("user") User user ,Model model){
            impl.addUser(user);
            return user;
        /*
            System.out.println(user.getUsername()+"--**--:"+user.getPassword());
            User u=impl.findUserById(1);
            model.addAttribute("user",user);*/
        }
        @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String toRegister(){
            return "register";
        }

        @RequestMapping(value = "/login",method = RequestMethod.POST)
        public String loginIn(@ModelAttribute("user") User user,Model model){
        User u=impl.findUserByName(user.getUsername());
        if(u==null){
            model.addAttribute("res","用户不存在");
            return "one";
        }
        if(u.getPassword().equals(user.getPassword())){
            return "startbootstrap/index.html";
        }else{
            model.addAttribute("res","密码错误");
            return "one";
        }
        }
}
